import { ElMessage } from "element-plus";
import axiosInstance from "../../plugins/axios";
import { FatherDTO,SecondDTO } from "../../type/Fp/Fp";


//整个表格数据获取
export const getFpInfo = async (taskId: string) => {
  const res = await axiosInstance.get("/second-demand/list");
  if (res.data.code == 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "获取需求功能数据失败");
    throw new Error(res.data.message || "获取需求功能数据失败");
  }
};

//通过taskId获取表格数据
export const getFpByTaskId = async (taskId: string) => {
  const res = await axiosInstance.get("/second-demand/listByTaskId", {
    params: { taskId }, 
  });
  if (res.data.code == 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "获取需求功能数据失败");
    throw new Error(res.data.message || "获取需求功能数据失败");
  }
};


//新增一级需求
export const addFatherDemand = async (fpFatherDemand: FatherDTO) => {
  const res = await axiosInstance.post("/fp-father-demand/add", fpFatherDemand);
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "添加一级需求失败");
    throw new Error(res.data.message || "添加一级需求失败");
  }
}

//修改一级需求
export const updateFatherDemand = async (fpFatherDemand: FatherDTO) => {
  const res = await axiosInstance.post("/fp-father-demand/update", fpFatherDemand);
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "更新一级需求失败");
    throw new Error(res.data.message || "更新一级需求失败");
  }
};

//删除一级需求
export const deleteBatch = (ids: number[]) => {
  return axiosInstance.post(
    "/fp-father-demand/deleteBatch",
    ids, // 直接发送数组
    {
      headers: {
        "Content-Type": "application/json",
      },
    }
  );
};



//新增二级需求
export const addSecondDemand = async (fpSecondDemand: SecondDTO) => {
  const res = await axiosInstance.post("/second-demand/add", fpSecondDemand);
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "添加二级需求失败");
    throw new Error(res.data.message || "添加二级需求失败");
  }
}

//修改二级需求
export const updateSecondDemand = async (fpSecondDemand: SecondDTO) => {
  const res = await axiosInstance.put("/second-demand/update", fpSecondDemand);
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "添加一级需求失败");
    throw new Error(res.data.message || "添加一级需求失败");
  }
}

//删除二级需求
export const deleteSecondDemand = async (id: number) => {
  try {
    const response = await axiosInstance.delete(`/second-demand/delete/${id}`);
    return response.data;
  } catch (error: any) {
    // 处理错误信息，可以根据后端接口返回的格式调整
    console.error('删除子级需求失败:', error);
    throw new Error(error.response?.data?.message || '删除失败');
  }
};

// 获取任务列表
export const getTaskList = async () => {
  try {
    const res = await axiosInstance.get("/tasks/allForFp"); // 调用后端接口
    if (res.data.code === 1) {
      return res.data.data; // 返回任务数据
    } else {
      ElMessage.error(res.data.message || "获取任务列表失败");
      throw new Error(res.data.message || "获取任务列表失败");
    }
  } catch (error) {
    ElMessage.error("获取任务列表失败");
    throw error; // 传递错误信息
  }
};

//根据一级需求查找
export const searchByPname = async (pname: string) => {
  const res = await axiosInstance.get("/second-demand/search-by-pname", {
    params: { pname },
  });
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "搜索失败");
    throw new Error(res.data.message || "搜索失败");
  }
};

// 根据二级名称查找
export const searchBySname = async (sname: string) => {
  try {
    // 使用 params 构造查询参数
    const res = await axiosInstance.get("/second-demand/search-by-name", {
      params: { name: sname },
    });

    if (res.data.code === 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "搜索失败");
      throw new Error(res.data.message || "搜索失败");
    }
  } catch (error) {
    console.error("API call failed:", error);
    throw error;
  }
};


//根据需求查找
export const searchByDescription = async (description: string) => {
  try {
    // 使用 params 构造查询参数
    const res = await axiosInstance.get("/second-demand/search-by-description", {
      params: { description },
    });

    if (res.data.code === 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "搜索失败");
      throw new Error(res.data.message || "搜索失败");
    }
  } catch (error) {
    console.error("API call failed:", error);
    throw error;
  }
};


//根据类型查找
export const searchByType = async (type: string) => {
  try {
    // 使用 params 构造查询参数
    const res = await axiosInstance.get("/second-demand/search-by-type", {
      params: { type },
    });

    if (res.data.code === 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "搜索失败");
      throw new Error(res.data.message || "搜索失败");
    }
  } catch (error) {
    console.error("API call failed:", error);
    throw error;
  }
};

//获取di
export const sendDI = async (taskId: string, diDictionary: Record<string, any>) => {
  // 构造请求数据，将 taskId 和 diDictionary 打包为一个对象
  const mapData = {
    taskId, // 任务 ID
    diDictionary, // DI 数据字典
  };

  // 使用 axios 发送 POST 请求
  const res = await axiosInstance.post("/di/task", mapData);

  // 检查后端响应状态
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "发送 DI 数据失败");
    throw new Error(res.data.message || "发送 DI 数据失败");
  }
};


//计算TotalUFP
export const cauclTotalUfp = async (taskId: string) => {
  try {
    const response = await axiosInstance.post(`/second-demand/cauclTotalUfp?taskId=${taskId}`);
    return response.data;
  } catch (error: any) {
    // 处理错误信息，可以根据后端接口返回的格式调整
    console.error('删除子级需求失败:', error);
    throw new Error(error.response?.data?.message || '删除失败');
  }
};

export const finallyCacul = async (taskId: string, totalufp:number,vaf:number,s:number,cf:number) => {
  // 构造请求数据，将 taskId 和 diDictionary 打包为一个对象
  const mapData = {
    taskId, // 任务 ID
    totalufp,
    vaf,
    s,
    cf
  };
  // 使用 axios 发送 POST 请求
  const res = await axiosInstance.post("/fpa/finallyCacul", mapData);

  // 检查后端响应状态
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "发送 DI 数据失败");
    throw new Error(res.data.message || "发送 DI 数据失败");
  }
};
